import { _decorator, Component, Label, Node, Sprite, tween } from 'cc';
import { ShowPanel } from './ShowPanel';
import { NUMBER, WeaponData, weaponProperty, WEAPONTYPE } from './GameConstant';
import { GameUtil } from './GameUtil';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { PrefsManager } from './PrefsManager';
import { MenuManager } from './MenuManager';
import { ShowWeapon } from './ShowWeapon';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('WarehousePanel')
export class WarehousePanel extends Component {

    @property(Node)
    PrevBtn: Node = null;

    @property(Node)
    NextBtn: Node = null;

    @property(Node)
    EquipNode: Node = null;

    @property({ type: Label, tooltip: "武器的初始伤害值" })
    InitialHarm: Label = null;

    @property({ type: Label, tooltip: "武器的初始射速" })
    InitialRate: Label = null;

    @property({ type: Label, tooltip: "武器的初始范围" })
    InitialRange: Label = null;

    @property({ type: Label, tooltip: "武器的增幅射速" })
    AmplifiesRate: Label = null;

    @property({ type: Label, tooltip: "武器的增幅范围" })
    AmplifiesRange: Label = null;

    @property({ type: Sprite, tooltip: "武器伤害值本身占比" })
    HarmSelfSprite: Sprite = null;

    @property({ type: Sprite, tooltip: "武器伤害值装备占比" })
    HarmPartSprite: Sprite = null;

    @property({ type: Sprite, tooltip: "武器射速本身占比" })
    RateSelfSprite: Sprite = null;

    @property({ type: Sprite, tooltip: "武器射速装备占比" })
    RatePartprite: Sprite = null;

    @property({ type: Sprite, tooltip: "武器射程本身占比" })
    RangeSelfSprite: Sprite = null;

    @property({ type: Sprite, tooltip: "武器射程装备占比" })
    RangePartSprite: Sprite = null;


    CurIndex: NUMBER = NUMBER.NUMBER1;

    showWindow(number: NUMBER) {
        ShowPanel.Instance.showWeapon(number);
    }

    showUI() {
        this.showBtn();
        this.showWindow(this.CurIndex);

        //展示武器的属性
        this.scheduleOnce(() => {
            const initialData: WeaponData = weaponProperty[ZTool.GetEnumKeyByValue(WEAPONTYPE, this.CurIndex)];
            const totalData: WeaponData = ShowWeapon.Instance.WeaponTS.WeaponData;
            this.updateFillRange(this.HarmSelfSprite, initialData.power / 10);
            this.updateFillRange(this.RateSelfSprite, initialData.rate / 100);
            this.updateFillRange(this.RatePartprite, totalData.rate / 100);
            this.updateFillRange(this.RangeSelfSprite, initialData.rate / 100);
            this.updateFillRange(this.RangePartSprite, totalData.rate / 100);
            this.InitialHarm.string = initialData.power.toString();
            this.InitialRate.string = initialData.rate.toString();
            this.InitialRange.string = initialData.range.toString();
            this.AmplifiesRate.string = Number((totalData.rate - initialData.rate).toFixed(1)) < 0 ? "0" : (totalData.rate - initialData.rate).toFixed(1);
            this.AmplifiesRange.string = Number((totalData.range - initialData.range).toFixed(1)) < 0 ? "0" : (totalData.range - initialData.range).toFixed(1);
        }, 0.1)
    }

    updateFillRange(sprite: Sprite, fillRange) {
        tween(sprite)
            .to(0.2, { fillRange: fillRange })
            .start();
    }



    showBtn() {
        if (this.CurIndex == NUMBER.NUMBER1) {
            this.PrevBtn.active = false;
            this.NextBtn.active = true;
        } else if (this.CurIndex == NUMBER.NUMBER5) {
            this.PrevBtn.active = true;
            this.NextBtn.active = false;
        } else {
            this.PrevBtn.active = true;
            this.NextBtn.active = true;
        }

        if (PrefsManager.Instance.userData.haveWeapon.findIndex(e => e == Number(this.CurIndex)) != -1) {
            this.EquipNode.active = true;
        } else {
            this.EquipNode.active = false;
        }
    }

    prevBtn() {
        AudioManager.PlaySound(Audios.Click);
        this.CurIndex = GameUtil.getAdjacentEnumCirculation(NUMBER, this.CurIndex, false);
        this.showUI();
    }

    nextBtn() {
        AudioManager.PlaySound(Audios.Click);
        this.CurIndex = GameUtil.getAdjacentEnumCirculation(NUMBER, this.CurIndex, true);
        this.showUI();
    }

    equipBtn() {
        AudioManager.PlaySound(Audios.Click);
        PrefsManager.Instance.userData.CurWeapon = WEAPONTYPE[ZTool.GetEnumKeyByValue(WEAPONTYPE, this.CurIndex)];
        PrefsManager.Instance.saveData();
        MenuManager.Instance.backBtn();
    }

    protected onEnable(): void {
        this.showUI();
    }

}


